package com.example.mapper;

import com.example.entity.Student;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

public interface StudentMapper {

    @Select("SELECT * FROM student WHERE username= #{username}")
    Student selectByUsername(String username);

    @Insert("INSERT INTO student VALUES(NULL,#{username},#{password},#{name},#{role}," +
            "#{sex},#{code},#{collegeId},#{score},#{avatar})")
    void insert(Student student);

    @Select("SELECT s.*,c.`name` collegeName FROM student s LEFT JOIN college c ON s.college_id=c.id")
    List<Student> selectAll();

    @Select("SELECT s.*,c.`name` collegeName FROM student s LEFT JOIN college c ON s.college_id=c.id " +
            "WHERE s.`name` LIKE CONCAT('%',#{name},'%')")
    List<Student> selectByName(String name);

    @Update("UPDATE student " +
            "SET username = #{username}, password = #{password}, name = #{name}," +
            " avatar = #{avatar}, role = #{role},  sex = #{sex}, code = #{code}," +
            "college_id = #{collegeId}, score = #{score} where id = #{id}")
    void updateById(Student student);

    @Select("SELECT s.*,c.`name` collegeName FROM student s LEFT JOIN college c ON s.college_id=c.id WHERE s.id=#{id}")
    Student selectById(Integer id);

    @Delete("delete from student where id=#{id}")
    void deleteById(Integer id);
}
